package com.damage.materiel_dept.web;

import com.damage.common.config.CodeConfig;
import com.damage.common.config.DepartmentConfig;
import com.damage.common.config.OrderConfig;
import com.damage.common.entity.Order;
import com.damage.common.entity.PurchaseInvoice;
import com.damage.common.exception.IncorrectPriceException;
import com.damage.common.exception.IncorrectStateException;
import com.damage.common.oa.completeOrder.CompleteOrderWorkflowService;
import com.damage.common.object.BaseResp;
import com.damage.common.service.IPublicService;
import com.damage.common.util.UserUtil;
import com.damage.materiel_dept.oa.completeOrder.RemarkInspectResultService;
import com.damage.materiel_dept.oa.completeOrder.StartCompleteOrderService;
import com.damage.materiel_dept.service.IPurchaseInvoiceService;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/purchaseInvoice")
@ResponseBody
@Slf4j
public class PurchaseInvoiceController {

    @Autowired
    private IPublicService publicService;

    @Autowired
    private StartCompleteOrderService startCompleteOrderService;

    @Autowired
    private RemarkInspectResultService remarkInspectResultService;

    @Autowired
    private CompleteOrderWorkflowService workflowService;

    @GetMapping("/todo")
    public BaseResp listTodoOrder(){
        List<Order> orders = publicService.findOrderByState(OrderConfig.DELIVERING);
        return BaseResp.ok(orders);
    }

    @PostMapping("/start")
    public BaseResp startPurchaseInvoice(PurchaseInvoice purchaseInvoice,
                                         HttpSession session){
        User user = UserUtil.getUserFromSession(session);
        if (user == null || Strings.isNullOrEmpty(user.getId())){
            //用户未登录
            return BaseResp.error(CodeConfig.ERROR_NOT_LOGIN);
        }
        if (!user.getFirstName().equals(purchaseInvoice.getInspector())){
            return BaseResp.error("采购计划的制定人和登录的用户不一致！");
        }
        PurchaseInvoice result = null;
        try {
            result = startCompleteOrderService.startCompleteOrder(purchaseInvoice);
        } catch (IncorrectStateException | IncorrectPriceException e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return BaseResp.error(e.getMessage());
        }
        return BaseResp.ok(result);
    }

    @PostMapping("remark/complete/{id}")
    public BaseResp completeRemarkInspectResult(@PathVariable("id")String taskId, String comment){
        try {
            remarkInspectResultService.remrkInspectResult(taskId,comment);
            return BaseResp.ok();
        } catch (Exception e) {
            log.error("完成任务失败，任务:{}",taskId);
            e.printStackTrace();
            return BaseResp.error(e.getMessage());
        }
    }

    @GetMapping("/list/task")
    public BaseResp taskList(HttpSession session){
        String userId = UserUtil.getUserFromSession(session).getId();
        List<PurchaseInvoice> invoiceList = workflowService.findTodoTasks(userId);
        return BaseResp.ok(invoiceList);
    }

    @GetMapping("/list/running")
    public BaseResp runnlingList(){
        List<PurchaseInvoice> invoiceList = workflowService.findRunningProcessInstaces();
        return BaseResp.ok(invoiceList);
    }

    @GetMapping("/list/finished")
    public BaseResp finishedList(){
        List<PurchaseInvoice> invoiceList = workflowService.findFinishedProcessInstaces();
        return BaseResp.ok(invoiceList);
    }


}
